package com.keith.brainmap.collections;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * 把流中的元素组合起来
 *
 * @author chenli
 * @date 2021-05-21 下午 03:29
 **/
public class ReduceStream {
    public static void main(String[] args) {
        Integer[] arr = {1, 2, 3, 4};
        List<Integer> list = Arrays.asList(arr);
        Optional<Integer> optional = list.stream().reduce(Integer::sum);
        System.out.println(optional.orElse(0));

        int reduce = list.stream().reduce(1, Integer::sum);
        System.out.println(reduce);

        List<String> list1 = new ArrayList<>();
        list1.add("我");
        list1.add("爱");
        list1.add("你");
        Optional<String> s = list1.stream().reduce(String::concat);
        System.out.println(s.orElse(""));
    }
}
